package com.cmc.cloud.cmclink.doc.util;

import com.cmc.cloud.cmclink.doc.constants.CommonConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.List;
import java.util.regex.Pattern;


/**
 * @author chenzheng
 * 2025/9/11
 **/
public class CheckUtil {

    private final static int CNTR_LENGTH = 11;
    private static final Pattern ENGLISH_CHAR_PATTERN = Pattern.compile("^[a-zA-Z0-9!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~\\s]*$");

    public static void checkString(String str, List<String> errorList) {
        if (CheckUtil.containsNoEnglish(str) || CheckUtil.isQuanJiaoValid(str)) {
            errorList.add(str);
        }
    }

    public static boolean containsNoEnglish(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        return !str.matches(
                "^(?=.*[a-zA-Z0-9])[a-zA-Z0-9\\s\\p{P}\\(\\)\\[\\]\\{\\}<>@$%^&*+\\-\\/=_,.:;!?~`|\\\\]*@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}[a-zA-Z0-9\\s\\p"
                        + "{P}\\(\\)\\[\\]\\{\\}<>@$%^&*+\\-\\/=_,.:;!?~`|\\\\]*$|^[a-zA-Z0-9\\s\\p{P}\\(\\)\\[\\]\\{\\}<>@$%^&*+\\-\\/=_,.:;!?~`|\\\\]+$");
    }

    /**
     * 检查字符串是否包含全角中文字符
     *
     * @param input 要检查的字符串
     * @return true-包含全角中文字符，false-不包含全角中文字符
     */
    public static boolean isQuanJiaoValid(String input) {
        if (input == null || input.isEmpty()) {
            return false;
        }

        // 匹配全角中文字符的正则表达式
        // 中文字符的Unicode范围：\u4E00-\u9FA5
        String regex = "[\u4E00-\u9FA5]";
        return Pattern.compile(regex).matcher(input).find();
    }

    /**
     * 检查是否包含中文字符
     * @param input input
     * @return boolean
     */
    public static boolean isAllEnValid(String input){
        return !ENGLISH_CHAR_PATTERN.matcher(input).matches();
    }

    /**
     * 检查箱号是否正确。11 位长度箱号有校验规则前四位字母后六位数字最后一位验证码也是数字
     */
    public static boolean isValidCntrNo(String cntrNo) {
        double quailtyNumber;
        double sum = 0;
        if (cntrNo.length() != CNTR_LENGTH) {
            return false;
        }
        char[] charArray = cntrNo.toCharArray();
        for (int i = 0; i < CommonConstants.FOUR; i++) {
            // 判断字符是否为大写字母
            if (!Character.isUpperCase(charArray[i])) {
                return false;
            } else {
                quailtyNumber = charArray[i] - 55d;
                for (int j = 1; j < CommonConstants.FOUR; j++) {
                    if (quailtyNumber >= j * CNTR_LENGTH) {
                        quailtyNumber++;
                    }
                }
                sum += quailtyNumber * Math.pow(2, i);
            }
        }
        if (charArray[CommonConstants.THREE] != CommonConstants.U) {
            return false;
        }
        for (int i = CommonConstants.FOUR; i < CommonConstants.TEN; i++) {
            // 判断字符是否为十进制数字
            if (!NumberUtils.isCreatable(String.valueOf(charArray[i]))) {
                return false;
            } else {
                quailtyNumber = Integer.parseInt(String.valueOf(charArray[i]));
                sum += quailtyNumber * Math.pow(2, i);
            }
        }
        return sum % CNTR_LENGTH == Double.parseDouble(String.valueOf(charArray[10]));
    }
}
